/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view.leftPanels;

import controller.Data;
import jsc.distributions.Normal;
import jsc.goodnessfit.KolmogorovTest;
import view.main.MainGUI;
import weka.core.Instances;

/**
 *
 * @author Stupi
 */
public class KolmogorovSmirnovPnlLeft extends javax.swing.JPanel {

    /**
     * Creates new form KolmogorovSmirnovPnlLeft
     */
    public KolmogorovSmirnovPnlLeft() {
        initComponents();
        
        setSize(MainGUI.getInstance().getPnlLeft().getWidth(), MainGUI.getInstance().getPnlLeft().getHeight());

        Instances instances = Data.getInstance().getInstances();

        int numAttriutes = instances.numAttributes();

        for (int i = 0; i < numAttriutes; i++) {

            if (instances.attribute(i).isNumeric()) {
                cmbAttributes.addItem(instances.attribute(i).name());
            }

        }

        setVisible(true);
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        cmbAttributes = new javax.swing.JComboBox();
        btnStart = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();

        jLabel1.setText("Select attribute:");

        btnStart.setText("Start");
        btnStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartActionPerformed(evt);
            }
        });

        textArea.setColumns(20);
        textArea.setRows(5);
        jScrollPane1.setViewportView(textArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(cmbAttributes, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnStart, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbAttributes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(btnStart)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 84, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartActionPerformed
        
        String selected = cmbAttributes.getSelectedItem().toString();

        Instances instances = Data.getInstance().getInstances();
        int N = instances.numInstances();
        double[] array = new double[N];

        mainLoop:
        for (int i = 0; i < instances.numAttributes(); i++) {
            if (instances.attribute(i).name().equals(selected)) {
                for (int j = 0; j < array.length; j++) {
                    array[j] = instances.instance(j).value(i);
                    break mainLoop;
                }
            }
        }
        
        
        Normal n = new Normal();
        KolmogorovTest k = new KolmogorovTest(array, n);
        
        double sign = k.getSP();
        
        String message;
        if (sign > 0.05 ) {
            message = "Hypothesis Ho is accepted\n\n"
                    + "This sample is from population\n"
                    + "with normal distribution ";
        }
        else
            message = "Hypothesis Ho is rejected\n\n"
                    + "This sample is not from population\n"
                    + "with normal distribution ";

        System.out.println("significant "+sign);
        textArea.setText(message);
        
        
        
    }//GEN-LAST:event_btnStartActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnStart;
    private javax.swing.JComboBox cmbAttributes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables
}
